package org.his.controller.xinwen;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.*;
import org.his.pojo.quanxuan.Role;
import org.his.pojo.quanxuan.User;
import org.his.pojo.xinwen.NContent;
import org.his.pojo.xinwen.NNew;
import org.his.pojo.xinwen.NPublishType;
import org.his.pojo.xinwen.NState;
import org.his.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/nnew")
public class NNewController {
    @Autowired
    private NNewDao nNewDao;

    @Autowired
    private NContentDao nContentDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private NPublishTypeDao nPublishTypeDao;

    @Autowired
    private NStateDao nStateDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "xinwen/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, NNew condition) throws Exception{
        Map<String, Object> map = new HashMap<>();
        Page<NNew> pager = PageHelper.startPage(page, limit);

        map.put("data", nNewDao.getList(condition));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);
        return map;
    }

    @RequestMapping("/form")
    public String from()throws Exception{
        return "xinwen/form";
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        nNewDao.delete(ids);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(String content,Integer publishTypeId ,Integer stateid, String title,  HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();

        NNew nNew = new NNew();
        nNew.setTitle(title);

        User login_user = (User) session.getAttribute(Common.LOGIN_USER);

        NPublishType nPublishType = new NPublishType();
        nPublishType.setTypeid(publishTypeId);

        NState nState = new NState();
        nState.setStateid(stateid);


        nNew.setUserid(login_user);
        nNew.setPublishTypeId(nPublishType);
        nNew.setStateid(nState);

        nNewDao.add(nNew);

        NContent nContent = new NContent();
        nContent.setNewid(nNew.getNewid());
        nContent.setContent(content);
        nContentDao.add(nContent);
        return map;
    }

    @RequestMapping(value = "/getType")
    @ResponseBody
    public List<NPublishType> getType() throws Exception{
       return nPublishTypeDao.getAll();
    }



    @RequestMapping(value = "/getState")
    @ResponseBody
    public List<NState> getState() throws Exception{
        return nStateDao.getAll();
    }

    @RequestMapping(value = "/shequ")
    public String shequ() throws Exception{
        return "xinwen/shequ";
    }

    @RequestMapping(value = "/getnews")
    public String getnews(Integer newid , ModelMap modelMap) throws Exception{

        modelMap.addAttribute("getnews",nNewDao.get(newid));


        return "xinwen/detail";


    }

}
